package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.http.BabyAllResponse;

import java.util.ArrayList;

public class ChangeAdapter extends RecyclerView.Adapter<ChangeViewHolder> implements ChangeViewHolder.OnClickChangeListener {

    private Context context;

    private ArrayList<BabyAllResponse> list;

    private int position;

    public ChangeAdapter(Context context) {
        this.context = context;
    }

    @Override
    public ChangeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_change, parent, false);
        ChangeViewHolder holder = new ChangeViewHolder(view);
        holder.setOnClickChangeListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(ChangeViewHolder holder, int position) {
        holder.setImageHeadUrl(list.get(position).getPath());
        holder.setName(list.get(position).getName());
        if (this.position == position) {
            holder.setSelectVisibility(true);
        } else {
            holder.setSelectVisibility(false);
        }
        switch (list.get(position).getAuth().getRole()) {
            case 0:
                holder.setRole(null);
                break;
            case 1:
                holder.setRole(context.getResources().getDrawable(R.drawable.ic_admin));
                break;
            case 2:
                holder.setRole(context.getResources().getDrawable(R.drawable.ic_supper));
                break;
        }
    }

    @Override
    public int getItemCount() {
        if (list == null || list.size() == 0) {
            return 0;
        }
        return list.size();
    }

    @Override
    public void onClickChange(ChangeViewHolder holder) {
        if (position == holder.getLayoutPosition()) {
            return;
        }
        position = holder.getLayoutPosition();
        notifyDataSetChanged();
    }

    public void setList(ArrayList<BabyAllResponse> list) {
        this.list = list;
    }

    public int getPosition() {
        return position;
    }

}
